
import { MoreWindow,MoreDocument } from '../type';
// 模拟事件 类似 fireKeyEvent(document.getElementById('thtml','keydown',122))
export const fireKeyEvent = (el, evtType, keyCode):any => {
    let evtObj;
    console.log(el);
    let win:MoreWindow = window;
    let Doc:MoreDocument = document;
    if (Doc.createEvent) {
        if (win.KeyEvent) { //firefox 浏览器下模拟事件
            evtObj = Doc.createEvent('KeyEvents');
            evtObj.initKeyEvent(evtType, true, true, win, true, false, false, false, keyCode, 0);
        } else { //chrome 浏览器下模拟事件
            evtObj = Doc.createEvent('UIEvents');
            evtObj.initUIEvent(evtType, true, true, win, 1);
            delete evtObj.keyCode;
            if (typeof evtObj.keyCode === "undefined") { //为了模拟keycode
                Object.defineProperty(evtObj, "keyCode", {
                    value: keyCode
                });
            } else {
                evtObj.key = String.fromCharCode(keyCode);
            }

            if (typeof evtObj.ctrlKey === 'undefined') { //为了模拟ctrl键
                Object.defineProperty(evtObj, "ctrlKey", {
                    value: true
                });
            } else {
                evtObj.ctrlKey = true;
            }
        }
        el.dispatchEvent(evtObj);

    } else if (Doc.createEventObject) { //IE 浏览器下模拟事件
        evtObj = Doc.createEventObject();
        evtObj.keyCode = keyCode
        el.fireEvent('on' + evtType, evtObj);
    }
};